<template id='WxPay'>
  <div class="WxPay">
    <go-to-back />
    <div class="top">
      <p class="tips">
        订单已确认，请尽快付款！订单号：<span>{{ order_sn }}</span>
      </p>
      <div class="price">
        应付金额:
        <p><span>¥</span>{{ productInfo.price }}</p>
      </div>
    </div>
    <div class="center">
      <p>微信支付</p>
      <div class="wxImg">
        <div class="wxCode">
          <div class="wxCodeImg">
            <vue-qr :text="baseURL" :qid="order_sn" :size="300"></vue-qr>
          </div>

          <div class="wxTips">
            <img :src="wxScanIcon" class="wxScanIcon" />
            <div>
              <p>请使用微信扫一扫</p>
              <p>扫描二维码支付</p>
            </div>
          </div>
        </div>
        <img :src="wxPayImg" class="wxBg" />
      </div>
    </div>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
import GoToBack from "./components/GoToBack.vue";
import { iscode, getWidth } from "@/utils";
import vueQr from "vue-qr";
import { getGoodsInfoApi, unifiedOrderApi, checkOrderApi } from "@/api";
export default {
  name: "WxPay",
  components: { GoToBack, vueQr },
  // 定义属性
  data() {
    return {
      productInfo: "",
      wxPayImg: require("@/assets/images/product/wxBg.png"),
      wxScanIcon: require("@/assets/images/product/scanIcon.png"),
      baseURL: "",
      order_sn: "",
      intervalId: "",
    };
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    async getGoodsInfo() {
      let result = await getGoodsInfoApi({ id: this.$route.query.id });
      if (iscode(result)) {
        this.productInfo = result.result.goods_info;
        this.wxpay();
      }
    },

    async wxpay() {
      let result = await unifiedOrderApi({
        order_sn: this.order_sn,
        pay_type: 2, //固定2微信
        platform: 1, //固定1pc
      });
      if (iscode(result)) {
        this.baseURL = result.result.code_url;
        this.checkOrder();
      }
    },

    checkOrder() {
      let that = this;
      that.intervalId = setInterval(async () => {
        let result = await checkOrderApi({
          order_sn: that.order_sn,
        });
        if (result.code == 0) {
          that.$message({
            message: result.message,
            iconClass: "mySuccessIcon",
            customClass: "mySuccess",
          });
          that.$router.push({
            path: "/payCompleted",
            query: {
              id: that.$route.query.id,
              order_id: that.$route.query.order_id,
            },
          });
        }
      }, 1000);
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.order_sn = this.$route.query.order_sn;
    this.getGoodsInfo();
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {
    clearInterval(this.intervalId);
  }, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang='less' scoped>
.WxPay {
  padding-bottom: 184px;
  p {
    margin: 0;
  }
  .top {
    margin-top: 30px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .tips {
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #ffffff;
    }
    .price {
      display: flex;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #ffffff;
      p {
        font-size: 16px;
        font-family: DINAlternate-Bold, DINAlternate;
        font-weight: bold;
        color: #00ffb0;
        margin-left: 10px;
        span {
          font-size: 12px;
          margin-right: 3px;
        }
      }
    }
  }
  .center {
    margin-top: 20px;
    width: 1200px;
    background: #2b2c2d;
    padding: 60px 0px 94px 100px;
    p {
      font-size: 16px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #ffffff;
    }
    .wxImg {
      margin-top: 30px;
      display: flex;
      .wxCode {
        display: flex;
        flex-direction: column;
        align-items: center;
        .wxCodeImg {
          width: 300px;
          height: 300px;
        }
        .wxTips {
          width: 100%;
          background: #212121;
          display: flex;
          // align-items: center;
          justify-content: center;
          padding: 30px 0;
          .wxScanIcon {
            width: 40px;
            height: 40px;
            margin-right: 20px;
          }
          p {
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #00ffb0;
          }
        }
      }
      .wxBg {
        width: 299px;
        margin-left: 220px;
      }
    }
  }
}
</style>